package yxy.game.pm2.command.arena.toparena;

import yxy.cherry.data.bean.dfsjingcai;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ArenaMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.arena.toparena.TopAreanHandle;
import yxy.game.pm2.module.arena.toparena.TopAreanModule;
import yxy.game.pm2.module.arena.toparena.TopAreanPlayer;
import yxy.game.pm2.module.arena.toparena.TopAreanStructure;
import yxy.game.pm2.module.player.PlayerHandle;

/**
 * 巅峰赛支持
 *
 * @ClassName CMD_TopArenaFan
 * @Author jingtao-zheng
 * @Date 2022/9/8 20:19
 * @Version 1.0
 **/
@MessageType(proto = Command.TopArenaFan, classtype = ArenaMessage.TopArenaFanRequest.class)
public class CMD_TopArenaFan extends Command<ArenaMessage.TopArenaFanRequest> {
    @Override
    public void execute(MessageSource messageSource, ArenaMessage.TopArenaFanRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        TopAreanHandle.TopAreanCode finalCode = TopAreanHandle.TopAreanCode.forNumber(request.getFinalCode().getNumber());
        int group = request.getGroup();
        String code = request.getCode();
        ArenaMessage.TopArenaFanResponse.Builder response = ArenaMessage.TopArenaFanResponse.newBuilder();
        try {
            PlayerBean playerBean = PlayerBean.get(playerCode);
            int serverId = playerBean.getServerId();
            ServerBean serverBean = ServerBean.get(serverId);
            int areaId = serverBean.getAreaId();
            TopAreanModule topAreanModule = TopAreanModule.get(areaId);
            try {
                if (topAreanModule.tryLock()) {
                    TopAreanStructure topAreanStructure = topAreanModule.matchGroup(group).get(finalCode);
                    TopAreanPlayer topAreanPlayer = topAreanStructure.matchPlayer(code);
                    if (topAreanPlayer.getFans().contains(playerCode)) {
                        send(messageSource, response, Result.KowFansHasVoted);//已经投过票
                    }
                    dfsjingcai data = topAreanStructure.data(group);
                    Currency cost = Currency.create(CurrencyType.CT_钱币, 0, data.getCostnum());
                    try {
                        new CurrencyHandle().cost(playerCode, CCReason.巅峰赛, cost);
                    } catch (CurrencyNotEnoughException e) {
                        send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));
                        return ;
                    }
                    topAreanPlayer.getFans().add(playerCode);
                    topAreanModule.set();
                    response.setFinalInfo(packFinalInfo(playerCode, topAreanStructure,group));
                    send(messageSource, response, Result.Success);
                }
            } catch (Exception e) {
                e.printStackTrace();
                send(messageSource, response, Result.DataError);
            } finally {
                topAreanModule.unlock();
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private ArenaMessage.TopArenaInfo packFinalInfo(String playerCode, TopAreanStructure topAreanStructure, int group) {
        ArenaMessage.TopArenaInfo.Builder infobuilder = ArenaMessage.TopArenaInfo.newBuilder();
        infobuilder.setFinalCode(yxy.game.pm2.message.ArenaMessage.TopArenaCode.forNumber(topAreanStructure.getFinalCode().getNumber()));
        {
            TopAreanPlayer finalPlayer = topAreanStructure.getPlayer1();
            ArenaMessage.TopArenaFighter.Builder builder = ArenaMessage.TopArenaFighter.newBuilder();
            builder.setFans(finalPlayer.getFans().size());
            builder.setLiked(finalPlayer.getFans().contains(playerCode));
            builder.setPlayerInfo(new PlayerHandle().playerSimpleInfo(finalPlayer.getPlayerCode()));
            builder.setGroup(group);
            infobuilder.addPlayers(builder);
        }
        {
            TopAreanPlayer finalPlayer = topAreanStructure.getPlayer2();
            ArenaMessage.TopArenaFighter.Builder builder = ArenaMessage.TopArenaFighter.newBuilder();
            builder.setFans(finalPlayer.getFans().size());
            builder.setLiked(finalPlayer.getFans().contains(playerCode));
            builder.setPlayerInfo(new PlayerHandle().playerSimpleInfo(finalPlayer.getPlayerCode()));
            builder.setGroup(group);
            infobuilder.addPlayers(builder);
        }
        if (topAreanStructure.getWinner() != null) {
            int battleResult = topAreanStructure.getWinner().getPlayerCode().equals(topAreanStructure.getPlayer1().getPlayerCode()) ? 1 : 2;
            infobuilder.setRecord(topAreanStructure.getRecord());
            infobuilder.setBattleResult(battleResult);
        }

        return infobuilder.build();
    }

    private void send(MessageSource messageSource, ArenaMessage.TopArenaFanResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
